<template>
  <div class="layout">
    <!-- 左侧竖式导航栏 -->
    <div class="side-navbar">
      <nav class="navbar">
        <a
          class="navbar-brand text-center"
          href="#"
        >教学管理中心</a>
        <div class="nav-buttons">
          <button
            type="button"
            class="btn menu-btn"
            :class="{ active: route.name === 'CourseMaintenanceEdit' }"
            @click="handleMenuItemClick('CourseMaintenanceEdit', $event)"
          >
            <el-icon class="menu-icon">
              <Notebook />
            </el-icon>
            <span>课程维护</span>
            <!-- 破裂碎片 -->
            <span class="shard shard-1" />
            <span class="shard shard-2" />
            <span class="shard shard-3" />
            <span class="shard shard-4" />
            <span class="shard shard-5" />
            <span class="shard shard-6" />
            <span class="shard shard-7" />
            <span class="shard shard-8" />
          </button>

          <button
            type="button"
            class="btn menu-btn"
            :class="{ active: route.name === 'ClassStudentsManagement' }"
            @click="handleMenuItemClick('ClassStudentsManagement', $event, route.params.classId)"
          >
            <el-icon class="menu-icon">
              <User />
            </el-icon>
            <span>班级学生管理</span>
            <span class="shard shard-1" />
            <span class="shard shard-2" />
            <span class="shard shard-3" />
            <span class="shard shard-4" />
            <span class="shard shard-5" />
            <span class="shard shard-6" />
            <span class="shard shard-7" />
            <span class="shard shard-8" />
          </button>

          <button
            type="button"
            class="btn menu-btn"
            :class="{ active: route.name === 'TeachingTeamManagement' }"
            @click="handleMenuItemClick('TeachingTeamManagement', $event, route.params.classId)"
          >
            <el-icon class="menu-icon">
              <Avatar />
            </el-icon>
            <span>教学团队管理</span>
            <span class="shard shard-1" />
            <span class="shard shard-2" />
            <span class="shard shard-3" />
            <span class="shard shard-4" />
            <span class="shard shard-5" />
            <span class="shard shard-6" />
            <span class="shard shard-7" />
            <span class="shard shard-8" />
          </button>
        </div>
      </nav>
    </div>

    <!-- 动态内容区域 -->
    <transition name="page">
      <div class="content-container">
        <router-view
          :key="route.fullPath"
          class="page"
        />
      </div>
    </transition>
  </div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router';
import {
  Notebook,
  User,
  Avatar
} from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();

const handleMenuItemClick = (routeName, event, classId = null) => {
  const button = event.target.closest('.menu-btn');
  if (button) {
    button.classList.add('breaking');
    setTimeout(() => {
      button.classList.remove('breaking');
    }, 500); 
  }

  goToRoute(routeName, classId);
};

const goToRoute = (name, classId = null) => {
  console.log(`TeachingManagement: 尝试导航到路由 "${name}"，传递 classId: ${classId}`);

  let targetRouteObj = { name: name };

  const currentClassIdInRoute = route.params.classId || route.query.classId;

  if (['ClassStudentsManagement', 'TeachingTeamManagement'].includes(name) && currentClassIdInRoute) {
    targetRouteObj.params = { classId: currentClassIdInRoute };
  }
  else if (['ClassStudentsManagement', 'TeachingTeamManagement'].includes(name) && classId) {
    targetRouteObj.params = { classId: classId };
  }

  let isSameRouteAndParams = route.name === name;
  if (targetRouteObj.params) {
    isSameRouteAndParams = isSameRouteAndParams && (route.params.classId === targetRouteObj.params.classId);
  } else {
    isSameRouteAndParams = isSameRouteAndParams && !route.params.classId; 
  }

  if (isSameRouteAndParams) {
    console.log(`已经在路由 "${name}" 且参数相同，无需重复导航。`);
    return;
  }

  router.push(targetRouteObj).catch(err => {
    if (err.name !== 'NavigationDuplicated' && err.name !== 'NavigationFailureType.duplicated') {
      console.error("Route navigation error:", err);
    }
  });
};
</script>

<style scoped>
.layout {
  display: flex;
  min-height: 100vh;
  background-color: #f5f7fa; 
  position: relative;
  /* 移除 overflow: hidden 以避免子元素被裁剪 */
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
}

.side-navbar {
  flex: 0 0 250px; 
  background-color: #3a4378; 
  padding: 2rem 1rem; /* 调整 padding */
  box-shadow: 4px 4px 12px rgba(0, 0, 0, 0.1); 
  height: 100vh;
  z-index: 100;
  display: flex; 
  flex-direction: column;
}

.navbar-brand {
  margin-bottom: 2rem;
  text-align: center;
  font-size: 1.75rem;
  font-weight: 700;
  color: #f8f9fd; 
  border-bottom: 2px solid #4a5386; 
  padding-bottom: 0.5rem;
}


.nav-buttons {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem; 
  padding-top: 1rem;
}

.menu-btn {
  position: relative;
  padding: 1rem 1.5rem; 
  border: none;
  border-radius: 0.75rem; 
  font-size: 1.125rem; 
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 0.75rem; 
  cursor: pointer;
  background-color: #404980; 
  color: #e0e3f0; 
  overflow: hidden; 
  z-index: 1; 
  transition: all 0.3s ease; 
}

.menu-btn .menu-icon {
  font-size: 1.5rem; 
  vertical-align: middle;
}

.menu-btn::before, .menu-btn::after {
  content: "";
  position: absolute;
  background-color: #6b76b0; 
  transition: all 0.6s ease;
  z-index: -1;
}
.menu-btn::before { width: 10px; height: 10px; top: -5px; left: -5px; transform: rotate(45deg); opacity: 0; }
.menu-btn::after { width: 10px; height: 10px; bottom: -5px; right: -5px; transform: rotate(-45deg); opacity: 0; }
.menu-btn:hover::before, .menu-btn:hover::after { opacity: 1; width: calc(100% + 10px); height: calc(100% + 10px); transform: rotate(0deg); top: 0; left: 0; }
.menu-btn:hover {
  color: #ffffff;
  box-shadow: 2px 2px 8px rgba(107, 118, 176, 0.2);
  transform: translateX(4px); 
  transition-delay: 0.1s; 
}

.menu-btn.active {
  background-color: #3498db; 
  color: #ffffff;
  font-weight: 600;
  box-shadow: 0 0 15px rgba(52, 152, 219, 0.4); 
  transform: translateX(0); 
}

.menu-btn.active::before { 
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  background-color: #409eff; 
  border-radius: 0 3px 3px 0;
  opacity: 1; 
  transform: none; 
  transition: none; 
}

.menu-btn.active::after { 
    opacity: 0;
    transition: none;
}


.content-container {
  flex-grow: 1;
  position: relative;
  height: 100vh;
  /* 移除 overflow: hidden 以避免子元素被裁剪 */
  padding: 20px;
  background-color: #ffffff; 
  border-radius: 12px;
  margin: 20px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
}

.page { 
  position: absolute; 
  width: calc(100% - 40px); 
  height: calc(100% - 40px); 
  transition: transform 0.5s ease-in-out, opacity 0.5s ease-in-out; 
  will-change: transform, opacity; 
  z-index: 10; 
}

.page-enter-active, .page-leave-active {

}
.page-enter-from { transform: translateX(100%); opacity: 0; }
.page-enter-to { transform: translateX(0); opacity: 1; }
.page-leave-from { transform: translateX(0); opacity: 1; }
.page-leave-to { transform: translateX(-100%); opacity: 0; }

.menu-btn .shard {
  position: absolute;
  top: 50%;
  left: 50%;
  background: #ecf0f1; 
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%); 
  transform-origin: center;
}

.menu-btn.breaking .shard-1 { animation: break-1 0.5s ease-out forwards; }
.menu-btn.breaking .shard-2 { animation: break-2 0.5s ease-out 0.08s forwards; }
.menu-btn.breaking .shard-3 { animation: break-3 0.5s ease-out 0.12s forwards; }
.menu-btn.breaking .shard-4 { animation: break-4 0.5s ease-out 0.16s forwards; }
.menu-btn.breaking .shard-5 { animation: break-5 0.5s ease-out 0.04s forwards; }
.menu-btn.breaking .shard-6 { animation: break-6 0.5s ease-out 0.1s forwards; }
.menu-btn.breaking .shard-7 { animation: break-7 0.5s ease-out 0.14s forwards; }
.menu-btn.breaking .shard-8 { animation: break-8 0.5s ease-out 0.18s forwards; }

@keyframes break-1 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(15deg) scale(1); }
  100% { opacity: 0; transform: translate(-40px, -45px) rotate(30deg) scale(0.2); }
}
@keyframes break-2 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(-20deg) scale(1); }
  100% { opacity: 0; transform: translate(35px, -30px) rotate(-10deg) scale(0.25); }
}
@keyframes break-3 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(45deg) scale(1); }
  100% { opacity: 0; transform: translate(-25px, 40px) rotate(60deg) scale(0.3); }
}
@keyframes break-4 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(-30deg) scale(1); }
  100% { opacity: 0; transform: translate(50px, 25px) rotate(-45deg) scale(0.22); }
}
@keyframes break-5 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(10deg) scale(1); }
  100% { opacity: 0; transform: translate(-15px, -50px) rotate(20deg) scale(0.28); }
}
@keyframes break-6 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(-15deg) scale(1); }
  100% { opacity: 0; transform: translate(20px, -40px) rotate(-5deg) scale(0.24); }
}
@keyframes break-7 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(30deg) scale(1); }
  100% { opacity: 0; transform: translate(-35px, 30px) rotate(45deg) scale(0.26); }
}
@keyframes break-8 {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(-25deg) scale(1); }
  100% { opacity: 0; transform: translate(45px, 35px) rotate(-30deg) scale(0.2); }
}

.menu-btn .shard-1 { width: 8px; height: 6px; border-radius: 1px; }
.menu-btn .shard-2 { width: 7px; height: 9px; border-radius: 2px; }
.menu-btn .shard-3 { width: 10px; height: 5px; border-radius: 3px; }
.menu-btn .shard-4 { width: 6px; height: 8px; border-radius: 1px; }
.menu-btn .shard-5 { width: 9px; height: 7px; border-radius: 2px; }
.menu-btn .shard-6 { width: 5px; height: 10px; border-radius: 3px; }
.menu-btn .shard-7 { width: 8px; height: 8px; border-radius: 1px; }
.menu-btn .shard-8 { width: 7px; height: 6px; border-radius: 2px; }

@media (max-width: 768px) {
  .side-navbar { flex: 0 0 auto; width: 100%; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); height: auto; }
  .nav-buttons { flex-direction: row; justify-content: center; gap: 1.5rem; padding: 1.5rem 0; }
  .menu-btn { padding: 0.75rem 1rem; font-size: 1rem; }
}
</style>
